const cloud = require("wx-server-sdk");

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });

const db = cloud.database();
const queryCheckInInfo = async (event) => {
  const { groupID } = event;

  if (!groupID) {
    return { success: false, error: "缺少分组ID" };
  }

  try {
    const group = await db.collection("Activity_Group_Information")
      .doc(groupID)
      .field({
        CheckInStartTime: 1,
        CheckInCodeOrURL: 1,
        IsCheckInActive: 1,
        CheckOutStartTime: 1,
        CheckOutCodeOrURL: 1,
        IsCheckOutActive: 1
      })
      .get();

    if (!group.data) {
      return { success: false, error: "未找到分组信息" };
    }

    return {
      success: true,
      checkInStartTime: group.data.CheckInStartTime,
      checkInCodeOrURL: group.data.CheckInCodeOrURL,
      isCheckInActive: group.data.IsCheckInActive,
      checkOutStartTime: group.data.CheckOutStartTime,
      checkOutCodeOrURL: group.data.CheckOutCodeOrURL,
      isCheckOutActive: group.data.IsCheckOutActive
    };
  } catch (error) {
    return {
      success: false,
      error: error.message || "查询签到/签退信息失败"
    };
  }
};

exports.main = async (event, context) => {
  return queryCheckInInfo(event);
};